<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>

var <c:out value='${controlId }' />addFile=new Array();//新添加文件的数组
var <c:out value='${controlId }' />delFile=new Array();//删除文件的数组
var <c:out value='${controlId }' />updateFile=new Array();//修改文件的数组
var _<c:out value='${controlId }'/>TotalSize="<c:out value='${totalSize}' />";//总附件大小

/*其他后台传过来参数说明：
addFlag;//上传按钮可用标识：0，不可用；1，可用
delFlag;//删除按钮显示标识：0，不显示；1，显示
showControlFlag;//file控件和上传按钮显示标识：0，不显示；1显示
controlId;//控件id
docId;//流程id
*/
var <c:out value='${controlId }' />FileLimitSize=209715200;//单个附件限制大小
var <c:out value='${controlId }' />FileLimitStr="200M";//单个附件限制大小文字，用于提示
var <c:out value='${controlId }' />AllFileLimitSize=1073741824;//总附件限制大小
var <c:out value='${controlId }' />AllFileLimitStr="1G";//总附件限制大小文字，用于提示

$(function(){
	_<c:out value='${controlId }'/>TotalSize=_<c:out value='${controlId }'/>TotalSize==""?0:_<c:out value='${controlId }'/>TotalSize;
	
	$("#<c:out value='${controlId }'/>File,#<c:out value='${controlId }'/>Btn").attr("disabled","<c:out value='${addFlag}' />"!="1");
	if("<c:out value='${showControlFlag}' />"=="1"){
		$("#<c:out value='${controlId }' />FileDiv").show();
	}else{
		$("#<c:out value='${controlId }' />FileDiv").hide();
	}
	
	//上传按钮点击事件
	$("#<c:out value='${controlId }' />Btn").click(function (){
		
		var attachFile=$("input[name='<c:out value='${controlId }' />File']").val();
		if(attachFile==null || attachFile==""){
			return;
		}
		var attachSuffix = attachFile.substring(attachFile.lastIndexOf(".") + 1, attachFile.length).toLowerCase();
		if(!(attachSuffix == "doc" || attachSuffix == "docx" || attachSuffix == "xls" 
				|| attachSuffix == "xlsx" || attachSuffix == "pdf"
				|| attachSuffix == 'ppt' || attachSuffix == 'pptx'
				|| attachSuffix == "jpeg" || attachSuffix == "jpg" 
				|| attachSuffix == "png" || attachSuffix == "bmp"
				|| attachSuffix == "gif" || attachSuffix == "zip"
				|| attachSuffix == "flv" || attachSuffix == "rar" || attachSuffix == "7z" || attachSuffix == "dwg")) {
			$.messager.alert("提示","附件只能上传word、excel、ppt、pdf、jpg、jpeg、png、gif、bpm、flv、rar、7z、zip、dwg文件。");
			return;
		}
		
		$("#<c:out value='${controlId }' />UploadStatus").show();
		
		$.ajaxFileUpload({
			url:"<c:url value='/synAttach/saveSynAttachToApp'/>", //需要链接到服务器地址
			secureuri:false,
			fileElementId:$("input[name='<c:out value='${controlId }' />File']").attr("id"), //文件选择框的id属性
			dataType: 'text', //服务器返回的格式，可以是json
			
			success: function (msg){ //相当于java中try语句块的用法
				var data = JSON.parse(decodeURIComponent(decodeURIComponent(msg)));
				if(data && data.state=="200"){
					if(Number(data.fileSize) > <c:out value='${controlId }' />FileLimitSize){
						$.messager.alert("提示","单个附件大小不能超过"+<c:out value='${controlId }' />FileLimitStr);
						$("#<c:out value='${controlId }' />UploadStatus").hide();
						$("#<c:out value='${controlId }' />File").filebox("clear");
						return;
					}
					if(Number(_<c:out value='${controlId }'/>TotalSize)+Number(data.fileSize) > <c:out value='${controlId }' />AllFileLimitSize){
						$.messager.alert("提示","上传附件总大小不能超过"+<c:out value='${controlId }' />AllFileLimitStr);
						$("#<c:out value='${controlId }' />UploadStatus").hide();
						$("#<c:out value='${controlId }' />File").filebox("clear");
						return;
					}else{
						_<c:out value='${controlId }'/>TotalSize=Number(_<c:out value='${controlId }'/>TotalSize)+Number(data.fileSize);
					}
					
					var fileObj=new Object();
					fileObj.fileSize=data.fileSize;
					fileObj.attachName=data.attachName;
					fileObj.suffixName=data.suffixName;
					fileObj.attachId=data.attachId;
					<c:out value='${controlId }' />addFile.push(fileObj);
					
					$("#<c:out value='${controlId }' />FileList").append("<li class=\"newFile\" synAttachId='"+fileObj.attachId+"'><a href=\"javascript:void(0)\" onclick=\"<c:out value='${controlId }' />CheckDocAttach(this,'"+fileObj.attachId+"','"+fileObj.suffixName+"','"+fileObj.attachName+"','<c:out value='${docId }' />')\">"+fileObj.attachName+fileObj.suffixName+"</a><a class=\"delAttach pointer attachImg\"  onclick=\"<c:out value='${controlId }' />DelFileFt(this)\"></a><a class=\"downloadAttach pointer attachImg\" onclick=\"<c:out value='${controlId }' />DownloadDocAttach(this,'"+fileObj.attachId+"','"+fileObj.suffixName+"','"+fileObj.attachName+"')\"></a></li>");
					
					if($("#<c:out value='${controlId }' />FileList li").size()>1){
						$("#<c:out value='${controlId }' />Bar").show();
					}else{
						$("#<c:out value='${controlId }' />Bar").hide();
					}
				}else{
					$.messager.alert("提示","上传失败,请重新上传!");
				}
				$("#<c:out value='${controlId }' />UploadStatus").hide();
				$("#<c:out value='${controlId }' />File").filebox("clear");
			},
			error: function (data, status, e) //相当于java中catch语句块的用法
			{
				if(e.number == "-2146827274"){
					$.messager.alert("提示","上传附件不符合要求，请检查附件！");
				}else{
					$.messager.alert("提示","上传失败,请重新上传!");
				}
				$("#<c:out value='${controlId }' />UploadStatus").hide();
				$("#<c:out value='${controlId }' />File").filebox("clear");
			}
		});
		
		
	});
	
	$("#<c:out value='${controlId }' />ShowDiv").parent("a").click(function(){
		$("#<c:out value='${controlId }' />ShowDiv").toggleClass("passwordbox-open").toggleClass("passwordbox-close");
		$("#<c:out value='${controlId }' />FileList li").not(':first').toggle();
	});
		
	//文件
	$("#<c:out value='${controlId }' />templateFile").filebox({  
        //required : true,  
        width : '90%',  
        multiple : true,  
        //validType : ['fileSize[1024,"kb"]' ],  
        buttonText : '浏览 ...',  
        buttonAlign : 'right',
        prompt : '请选择一个文件...',  
        //accept : 'application/msword'  
});  
	$("#<c:out value='${controlId }' />ReplaceDiv").dialog({
		title:"替换附件",//标题
		width: 800,//宽度
	    height: 170,//高度
	    closed: true,//初始关闭
	    cache: true,//缓存
	    modal: true,
	    href: "",
	    buttons:[{
			text:'确定',
			handler:function(){
				var attachFile=$("input[name='<c:out value='${controlId }' />templateFile']").val();
				if(attachFile==null || attachFile==""){
					return;
				}
				var attachSuffix = attachFile.substring(attachFile.lastIndexOf(".") + 1, attachFile.length).toLowerCase();
				if(!(attachSuffix == "doc" || attachSuffix == "docx" || attachSuffix == "xls" 
						|| attachSuffix == "xlsx" || attachSuffix == "pdf"
						|| attachSuffix == 'ppt' || attachSuffix == 'pptx'
						|| attachSuffix == "jpeg" || attachSuffix == "jpg" 
						|| attachSuffix == "png" || attachSuffix == "bmp"
						|| attachSuffix == "gif" || attachSuffix == "zip"
						|| attachSuffix == "flv" || attachSuffix == "rar" || attachSuffix == "7z" || attachSuffix == "dwg")) {
					$.messager.alert("提示","附件只能上传word、excel、ppt、pdf、jpg、jpeg、png、gif、bpm、flv、rar、7z、zip、dwg文件。");
					return;
				}
				
				$("#<c:out value='${controlId }' />UploadStatus").show();
				
				$.ajaxFileUpload({
					url:"<c:url value='/synAttach/saveSynAttachToApp'/>", //需要链接到服务器地址
					secureuri:false,
					fileElementId:$("input[name='<c:out value='${controlId }' />templateFile']").attr("id"), //文件选择框的id属性
					dataType: 'text', //服务器返回的格式，可以是json
					
					success: function (msg){ //相当于java中try语句块的用法
						var data = JSON.parse(decodeURIComponent(decodeURIComponent(msg)));
						if(data && data.state=="200"){
							if(Number(data.fileSize) > <c:out value='${controlId }' />FileLimitSize){
								$.messager.alert("提示","单个附件大小不能超过"+<c:out value='${controlId }' />FileLimitStr);
								$("#<c:out value='${controlId }' />UploadStatus").hide();
								$("#<c:out value='${controlId }' />File").filebox("clear");
								return;
							}
								//修改附件暂时不考虑总大小，后期修改
//							if(Number(_<c:out value='${controlId }'/>TotalSize)+Number(data.fileSize) > <c:out value='${controlId }' />AllFileLimitSize){
//								$.messager.alert("提示","上传附件总大小不能超过"+<c:out value='${controlId }' />AllFileLimitStr);
//								$("#<c:out value='${controlId }' />UploadStatus").hide();
//								$("#<c:out value='${controlId }' />File").filebox("clear");
//								return;
//							}else{
//								_<c:out value='${controlId }'/>TotalSize=Number(_<c:out value='${controlId }'/>TotalSize)+Number(data.fileSize);
//							}
							
							var fileObj=new Object();
							fileObj.fileSize=data.fileSize;
							fileObj.attachName=data.attachName;
							fileObj.suffixName=data.suffixName;
							fileObj.attachId=data.attachId;
							fileObj.attachFlag=$("#<c:out value='${controlId }' />ReplaceId").val();
							<c:out value='${controlId }' />updateFile.push(fileObj);
							
							$("#<c:out value='${controlId }'/>FileList").find("li[updateId='"+fileObj.attachFlag+"']").replaceWith("<li class=\"newFile\" synAttachId='"+fileObj.attachId+"' updateId='"+fileObj.attachFlag+"'><a href=\"javascript:void(0)\" onclick=\"<c:out value='${controlId }' />CheckDocAttach(this,'"+fileObj.attachId+"','"+fileObj.suffixName+"','"+fileObj.attachName+"','<c:out value='${docId }' />')\">"+fileObj.attachName+fileObj.suffixName+"</a><a class='updateAttach pointer attachImg'  onclick='<c:out value="${controlId }" />updateFileFt(this)'></a><a class=\"delAttach pointer attachImg\"  onclick=\"<c:out value='${controlId }' />DelFileFt(this)\"></a><a class=\"downloadAttach pointer attachImg\" onclick=\"<c:out value='${controlId }' />DownloadDocAttach(this,'"+fileObj.attachId+"','"+fileObj.suffixName+"','"+fileObj.attachName+"')\"></a></li>");

					}else{
							$.messager.alert("提示","上传失败,请重新上传!");
						}
						$("#<c:out value='${controlId }' />UploadStatus").hide();
						$("#<c:out value='${controlId }' />templateFile").filebox("clear");
						$("#<c:out value='${controlId }' />").val("");
						$("#<c:out value='${controlId }' />ReplaceDiv").dialog("close");
						
					},
					error: function (data, status, e) //相当于java中catch语句块的用法
					{
						if(e.number == "-2146827274"){
							$.messager.alert("提示","上传附件不符合要求，请检查附件！");
						}else{
							$.messager.alert("提示","上传失败,请重新上传!");
						}
						$("#<c:out value='${controlId }' />UploadStatus").hide();
						$("#<c:out value='${controlId }' />templateFile").filebox("clear");
						$("#<c:out value='${controlId }' />").val("");
						$("#<c:out value='${controlId }' />ReplaceDiv").dialog("close");
					}
				});
			}
		},{
			text:'关闭',
			handler:function(){$("#<c:out value='${controlId }' />ReplaceDiv").dialog("close");}
		}]
	});
});
//删除按钮绑定事件
function <c:out value='${controlId }' />DelFileFt(control) {
	$.messager.confirm('提示', "确定要删除该附件吗？", function(r){
		if (r){
			var $li=$(control).parent("li");
			var attachId=$li.attr("synAttachId");
			if("oldFile"==$li.attr("class")){
				<c:out value='${controlId }' />delFile.push(attachId);
			}else{
				$.each( <c:out value='${controlId }' />addFile, function(i, n){
					  if(n.attachId==attachId){
						  <c:out value='${controlId }' />addFile.splice(i,1);
					  }
				});
			}
			$li.remove();
			if($("#<c:out value='${controlId }' />FileList li").size()>1){
				$("#<c:out value='${controlId }' />Bar").show();
			}else{
				$("#<c:out value='${controlId }' />Bar").hide();
			}
		}
	});
};
//替换按钮绑定事件
function <c:out value='${controlId }' />updateFileFt(control) {
	var updateId=$(control).parent("li").attr("updateId");
	if(updateId!=""){
		$("#<c:out value='${controlId }' />ReplaceId").val(updateId);
		$("#<c:out value='${controlId }' />ReplaceDiv").dialog("open").window("center");
	}
};

//保存文件信息，并且上传文件到ftp
function <c:out value='${controlId }' />SaveFileFt(docId,attachNo,attachVersion,attachType,workEffortId){
	var attachFile=$("input[name='<c:out value='${controlId }' />File']").val();
	
	
	if(attachFile!=null && attachFile!=""){
		$.messager.alert("提示","附件未上传，请点击上传按钮上传！");
		return false;
	}else{
		var flag=false;
		if(<c:out value='${controlId }' />addFile.length==0 && <c:out value='${controlId }' />delFile.length==0 && <c:out value='${controlId }' />updateFile.length==0){
			return true;
		}
		
		$.ajax({
			   type: "POST",
			   url: "<c:url value='/synAttach/changeAllSynAttach' />",
			   async: false,
			   data: "docId="+docId+"&attachNo="+attachNo+"&attachVersion="+attachVersion
			   +"&attachType="+attachType+"&workEffortId="+workEffortId
			   +"&addFileList="+(<c:out value='${controlId }' />addFile.length==0?"":encodeURIComponent(encodeURIComponent(JSON.stringify(<c:out value='${controlId }' />addFile))))
			   +"&updateFileList="+(<c:out value='${controlId }' />updateFile.length==0?"":encodeURIComponent(encodeURIComponent(JSON.stringify(<c:out value='${controlId }' />updateFile))))
			   +"&delAttachId="+(<c:out value='${controlId }' />delFile.length==0?"":JSON.stringify(<c:out value='${controlId }' />delFile)),
			   success: function(msg){
				   if(msg && msg.state=="200"){
					   $.each(  <c:out value='${controlId }' />addFile, function(i, n){
						   $("#<c:out value='${controlId }'/>FileList li[synAttachId='"+n.attachId+"']").attr("class","oldFile");
						 });
					   
					   <c:out value='${controlId }' />addFile.length=0;
					   <c:out value='${controlId }' />delFile.length=0;
					   flag=true;
				   }
			   }
			});
	}
	
	
		return flag;
}
//打开附件（如果为office文件）或者下载附件
function <c:out value="${controlId }" />CheckDocAttach(control,attachId,suffixName,attachName,docId){
	var newFlag="";
	if("newFile"==$(control).parent("li").attr("class")){
		newFlag="1";
	}else{
		newFlag="0";
	}
	var attachSuffix = suffixName.substring(suffixName.lastIndexOf(".") + 1, suffixName.length).toLowerCase();
	if(attachSuffix == "doc" || attachSuffix == "docx" || attachSuffix == "xls" 
			|| attachSuffix == "xlsx" || attachSuffix == 'ppt' 
				|| attachSuffix == 'pptx' || attachSuffix == "pdf"  ) {
		
		var vHeight = window.screen.height -73 ;
		var fileReadOnly="<c:out value='${fileReadOnly}' />";
		var controlId='<c:out value="${controlId }" />';
		var wordUrl =_synergyContext+"/basic/office/gotoAttachOfficePage?attachId=" + attachId + "&suffixName="+suffixName+"&docId="+docId
		+"&newFlag="+newFlag+"&fileReadOnly="+fileReadOnly+"&controlId="+controlId+"&notkHeigth="+vHeight
		+"&limitNum="+<c:out value='${controlId }' />FileLimitSize+"&limitStr="+<c:out value='${controlId }' />FileLimitStr
		+"&limitNumAll="+<c:out value='${controlId }' />AllFileLimitSize+"&limitStrAll="+<c:out value='${controlId }' />AllFileLimitStr
		+"&limitTotalNum="+_<c:out value='${controlId }'/>TotalSize+ "&attachName="+encodeURIComponent(encodeURIComponent(attachName));
		/*var winFeatures = "height="+vHeight+"px,width="+(window.screen.width-20)+"px,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no";
		window.open(wordUrl,'_blank',winFeatures);*/
		ntkoBrowser.openWindow(wordUrl);
	}else{
		window.location.href =_synergyContext+"/basic/synAttach/downLoadFile" + "?attachId=" + attachId + "&attachName="+encodeURIComponent(encodeURIComponent(attachName))+"&suffixName="+suffixName+"&newFlag="+newFlag;
	}
}

//下载附件
function <c:out value="${controlId }" />DownloadDocAttach(control,attachId,suffixName,attachName){
	var newFlag="";
	if("newFile"==$(control).parent("li").attr("class")){
		newFlag="1";
	}else{
		newFlag="0";
	}
	
    var param = "attachId=" + attachId + "&attachName="+encodeURIComponent(encodeURIComponent(attachName))+"&suffixName="+suffixName+"&newFlag="+newFlag;
    //window.location.href ="<c:url value='/basic/synAttach/downLoadFile' />" + "?attachId=" + attachId + "&attachName="+attachName+"&suffixName="+suffixName+"&newFlag="+newFlag;
    window.location.href =_synergyContext+"/basic/synAttach/downLoadFile" + "?"+param;
}

//修改总附件大小，编辑附件的时候使用
function <c:out value="${controlId }" />ChangeTotalSize(value){
	_<c:out value='${controlId }'/>TotalSize=Number(_<c:out value='${controlId }'/>TotalSize)+Number(value);
}
//获取附件总大小，编辑附件的时候使用
function <c:out value="${controlId }" />GetTotalSize(){
	return _<c:out value='${controlId }'/>TotalSize;
}
//获取附件限制大小，编辑附件的时候使用
function <c:out value='${controlId }' />GetFileLimitSize(){
	return <c:out value='${controlId }'/>FileLimitSize;
}
//获取附件限制大小文字，编辑附件的时候使用
function <c:out value='${controlId }' />GetFileLimitStr(){
	return <c:out value='${controlId }'/>FileLimitStr;
}
//获取总附件限制大小，编辑附件的时候使用
function <c:out value='${controlId }' />GetAllFileLimitSize(){
	return <c:out value='${controlId }'/>AllFileLimitSize;
}
//获取总附件限制大小文字，编辑附件的时候使用
function <c:out value='${controlId }' />GetAllFileLimitStr(){
	return <c:out value='${controlId }'/>AllFileLimitStr;
}

//获取已上传文件个数，验证时候用
function <c:out value='${controlId }'/>GetAllFileNum(){
	return $("#<c:out value='${controlId }'/>FileList").find("li").size();
}